﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using BlogsAPI.Abstract;
using Newtonsoft.Json.Linq;

namespace BlogsAPI.Images
{
    /// <summary>
    /// Популярный мобильный фотохостинг
    /// </summary>
    [SingleImager(Link = "https://www.instagram.com")]
    public class Instagram : ImageServer
    {
        /// <summary>
        /// Получение превьюшки 
        /// </summary>
        /// <param name="sLink"></param>
        /// <returns></returns>
        public override string GetThumb(string sLink)
        {
            string MediaLinked = String.Empty;
            //https://api.instagram.com/oembed?url={url}&maxwidth=320
            try
            {
                //{
                //  "provider_url": "http://instagram.com/",
                //  "media_id": "206830900451976720_49825232",
                //  "title": "#abstract #texture of #chair #surface",
                //  "thumbnail_height": 306,
                //  "html": "<blockquote class=\"instagram-media\" data-instgrm-captioned data-instgrm-version=\"4\" style=\" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:400px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);\"><div style=\"padding:8px;\"> <div style=\" background:#F8F8F8; line-height:0; margin-top:40px; padding:50% 0; text-align:center; width:100%;\"> <div style=\" background:url(); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;\"></div></div> <p style=\" margin:8px 0 0 0; padding:0 4px;\"> <a href=\"https://instagram.com/p/Lez5uuA6YQ/\" style=\" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;\" target=\"_top\">#abstract #texture of #chair #surface</a></p> <p style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;\">A photo posted by Andrey Moseev (@amoses_md) on <time style=\" font-family:Arial,sans-serif; font-size:14px; line-height:17px;\" datetime=\"2012-06-05T06:03:06+00:00\">Jun 6, 2012 at 11:03pm PDT</time></p></div></blockquote>\n<script async defer src=\"//platform.instagram.com/en_US/embeds.js\"></script>",
                //  "author_name": "amoses_md",
                //  "height": null,
                //  "thumbnail_width": 306,
                //  "width": 400,
                //  "version": "1.0",
                //  "author_url": "http://instagram.com/amoses_md",
                //  "author_id": 49825232,
                //  "thumbnail_url": "http://distilleryimage0.ak.instagram.com/1e71d7faaed411e19894123138140d8c_6.jpg",
                //  "type": "rich",
                //  "provider_name": "Instagram"
                //}
                using (var client = new WebClient { Encoding = Encoding.UTF8 })
                {
                    Stream stream =
                        client.OpenRead(String.Format("https://api.instagram.com/oembed?url={0}&maxwidth=320", sLink));
                    var reader = new StreamReader(stream);
                    JToken token = JObject.Parse(reader.ReadToEnd());

                    UserID = token["author_id"].Value<string>(); //+
                    UserName = token["author_name"].Value<string>(); //+
                    MediaID = token["media_id"].Value<string>(); //+               
                    Height = token["height"].Value<string>(); //+
                    Width = token["width"].Value<string>(); //+
                    Title = token["title"].Value<string>(); //+
                    //can be empty???
                    MediaLinked = token["thumbnail_url"].Value<string>(); //fixed
                }
            }
            catch (Exception e1)
            {
                Debug.WriteLine(e1.Message);
            }
            return MediaLinked;
        }
    }
}
